﻿using System.Text;
using RimWorld;
using UnityEngine;
using Verse;

namespace RimZombieGirl
{
    public class Alert_CurseInfo : Alert
    {
        private MapComponent_Curse CompCurse
        {
            get
            {
                return Find.CurrentMap?.GetComponent<MapComponent_Curse>();
            }
        }

        private const float TicksPerDay = 60000f;

        public Alert_CurseInfo()
        {
            this.defaultLabel = "诅咒值";
            this.defaultPriority = AlertPriority.Medium;
        }

        public override AlertReport GetReport()
        {
            if (CompCurse == null || CompCurse.currentCurseValue <= 0f)
            {
                return AlertReport.Inactive;
            }
            return AlertReport.Active;
        }

        public override string GetLabel()
        {
            return $"诅咒值：{CompCurse.currentCurseValue:F0}";
        }

        public override TaggedString GetExplanation()
        {
            StringBuilder sb = new StringBuilder();

            float cur = CompCurse.currentCurseValue;
            float target = CompCurse.targetCurseValue;
            float diff = Mathf.Abs(target - cur);

            // 每 tick 变化速度
            float perTick = MapComponent_Curse.DailyChange / TicksPerDay;
            int ticksNeeded = perTick > 0.0001f ? (int)(diff / perTick) : 0;

            sb.AppendLine("当前诅咒值".Translate() + $": {cur:F0}");
            sb.AppendLine("目标诅咒值".Translate() + $": {target:F0}");

            if (cur != target)
            {
                sb.AppendLine($"预计 {(cur < target ? "增长" : "回落")} 至目标需要: {ticksNeeded.ToStringTicksToPeriod(true, false, true, true, false)}");
            }
            else
            {
                sb.AppendLine("已到达目标诅咒值");
            }

            // 当前频率
            float meanDays = 10f * (500f / (cur + 1f));
            float meanTicks = meanDays * TicksPerDay;
            sb.AppendLine($"邪祟事件触发间隔（平均）: {((int)meanTicks).ToStringTicksToPeriod(true, false, true, true, false)}");

            return sb.ToString();
        }
    }
}
